import axios from 'axios'
import { ElMessage } from 'element-plus'

const request = axios.create({
	baseURL: '/',  
    timeout: 5000
})

// request 拦截器
// 可以自请求发送前对请求做一些处理
// 比如统一加token，对请求参数统一加密
request.interceptors.request.use(config => {
    config.headers['Content-Type'] = 'application/json;charset=utf-8';
    if(localStorage.getItem('access_token')!=null)config.headers['Authorization']='Bearer '+localStorage.getItem('access_token')
 // config.headers['token'] = user.token;  // 设置请求头
    return config
}, error => {
    return Promise.reject(error)
});

// response 拦截器
// 可以在接口响应后统一处理结果
request.interceptors.response.use(
    response => {
        let res = response.data;
        if(res.success==false){
            ElMessage({
                message: res.msg,
                type: "error",
            })
        }
        if(res.code==24161){
            console.log('token过期了');
            // request.post('/refresh_token',JSON.stringify({refresh_token:localStorage.getItem('refresh_token')})).then(data=>{
            //     console.log(data);
            // })
        }
        return res;
    },
    error => {
        if(error.response.status!=403)
        ElMessage({
            message: error,
            type: "error",
        })
        return Promise.reject(error)
    }
)


export default request